/*
 * TableParser.java
 */
package mango.orm.entity.maker;

import java.util.Date;
import java.util.List;
import java.util.Random;

import mango.Mango;
import mango.functions.FuncDate;
import mango.functions.FuncString;
import mango.orm.OrmTypes;
import mango.orm.entity.Entity;



/**
 * 将Table转换成实体类的字符串
 * 
 * @author zhangms
 */
public class EOTableParser {
    public static String parse(EOTable table) {
        StringBuilder entityStr = new StringBuilder();
        entityStr.append(EOTableParser.makeHeadStr(table));

        List<EOColumn> columns = table.getColumns();
        for (EOColumn column : columns) {
            String getMethodStr = EOTableParser.makeGetMethodStr(column);
            entityStr.append(getMethodStr);
            String setMethodStr = EOTableParser.makeSetMethodStr(column, table.getJavaClassName());
            entityStr.append(setMethodStr);
        }
        entityStr.append(EOTableParser.getEntityStrTail());
        return entityStr.toString();
    }

    private static String makeSetMethodStr(EOColumn column, String className) {
        StringBuilder ret = new StringBuilder();
        ret.append("    public ").append(className).append(" set")
                .append(FuncString.firstUpper(column.getCode()));
        ret.append("(").append(column.getJavaType()).append(" ")
                .append(FuncString.firstLower(column.getCode())).append(") {\n");
        ret.append("        this.set(\"").append(column.getCode().toUpperCase()).append("\", ")
                .append(FuncString.firstLower(column.getCode())).append(");\n");
        ret.append("        return this;\n");
        ret.append("    }\n\n");
        return ret.toString();
    }

    private static String makeGetMethodStr(EOColumn column) {
        StringBuilder ret = new StringBuilder();
        ret.append("    /**\n");
        ret.append("     * Name    : ").append(FuncString.null2Blank(column.getName())).append("\n");
        ret.append("     * Code    : ").append(FuncString.null2Blank(column.getCode())).append("\n");
        ret.append("     * Comment : ").append(FuncString.null2Blank(column.getComment())).append("\n");
        ret.append("     **/\n");
        ret.append("    @Column(column=\"").append(column.getCode().toUpperCase()).append("\"")
        .append(", primary=").append(column.isPrimary()).append(", type=")
        .append(column.getJdbcType()).append(")\n");
        ret.append("    public ").append(column.getJavaType()).append(" get")
                .append(FuncString.firstUpper(column.getCode())).append("() {\n");
        ret.append("        return ").append(OrmTypes.class.getSimpleName()).append(".convert(").append(column.getJavaType())
                .append(".class, ").append("this.get(\"").append(column.getCode().toUpperCase())
                .append("\"));\n");
        ret.append("    }\n\n");
        return ret.toString();
    }

    private static String makeHeadStr(EOTable table) {
        StringBuilder ret = new StringBuilder();
        ret.append("/*\n").append(" *Copyright (c) ")
                .append(FuncDate.getCurrentDate("yyyy")).append(" ")
                .append(Mango.class.getSimpleName())
                .append(" Persistence Model").append("\n */\n");
        ret.append("package ").append(table.getPkg()).append(";\n\n");
        ret.append("import ").append(Entity.class.getName()).append(";\n");
        ret.append("import ").append(mango.orm.entity.Table.class.getName()).append(";\n");
        ret.append("import ").append(mango.orm.entity.Column.class.getName()).append(";\n");
        ret.append("import ").append(mango.orm.OrmTypes.class.getName()).append(";\n");
        ret.append("import java.sql.Types;\n");
        String tableComment = table.getName();
        if (table.getComment() != null) {
            tableComment = table.getComment();
        }
        ret.append("\n").append("/**\n").append(" * " + tableComment + "\n").append(" * @serial ")
                .append(new Date().getTime()).append("\n */").append("\n");
        ret.append("@Table(\"").append(table.getCode().toUpperCase()).append("\")\n");
        ret.append("public class ").append(table.getJavaClassName()).append(" extends Entity {\n");
        ret.append("    private static final long serialVersionUID = -")
                .append(new Date().getTime()).append(new Random().nextInt(99999)).append("L;\n\n");
        return ret.toString();
    }

    private static String getEntityStrTail() {
        return "}\n";
    }
}
